package com.sks.service.house.impl;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sks.bean.pojo.Area;
import com.sks.dao.house.AreaDao;
import com.sks.service.base.BaseService;
import com.sks.service.house.AreaService;

@Service
@Transactional
public class AreaServiceBean extends BaseService<Area> implements AreaService {

	@Resource(name = "areaDao")
	public void setAreaDao(AreaDao areaDao) {
		this.setDao(areaDao);
	}

	/**
	 * @param visible
	 * */
	public List<Area> findTopArea(Boolean visible) {
		AreaDao areaDao = (AreaDao)this.getDao();
		StringBuffer jpql = new StringBuffer(" o.parentArea is null");
		if (null == visible) {
			return areaDao.getScrollData(-1, -1, jpql.toString(), null).getResultlist();
		} else {
			jpql.append(" and o.visible = ?1");
			return areaDao.getScrollData(-1, -1, jpql.toString(),
					new Object[] { visible }).getResultlist();
		}
	}
	
	public List<Area> findAreas(Boolean isTop, Boolean visible) {
		AreaDao areaDao = (AreaDao)this.getDao();
		StringBuffer jpql = new StringBuffer(" 1 = 1 ");
		if (null != isTop) {
			jpql.append(isTop ? " and o.parentArea is null " : " and o.parentArea is not null "); 
		}
		if (null == visible) {
			return areaDao.getScrollData(-1, -1, jpql.toString(), null).getResultlist();
		} else {
			jpql.append(" and o.visible = ?1 ");
			return areaDao.getScrollData(-1, -1, jpql.toString(),
					new Object[] { visible }).getResultlist();
		}
	}
	
	public List<Area> findAreas(Boolean isTop) {
		return findAreas(isTop, null);
	}
	
	public Area getDefaultTopArea() {
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>(0);
		orderby.put("areaId", "asc");
		List<Area> areas = this.getDao().getScrollData(0, 1, " o.parentArea is null ", null, orderby).getResultlist();
		if (null != areas && areas.size() == 1) {
			return areas.get(0);
		}
		return null;
	}
}
